﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using My.Core.UnifyResult.Fiters;
using My.Framework.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace My.Core.UnifyResult
{
	/// <summary>
	/// 采用furion格式的规范化api
	/// 兼容一套类似furion的返回情况，200状态码包一层更符合国内习惯，既然如此，不如直接搬过来
	/// </summary>
	public class UnifyResultStartup : StartupModule<UnifyResultStartup>
	{
		/// <summary>
		/// 程序启动器
		/// </summary>
		public UnifyResultStartup() => Order = 1;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="webApplicationBuilder"></param>
		public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
		{
			var services = webApplicationBuilder.Services;

			//成功规范接口
			services.AddTransient<SucceededUnifyResultFilter>();
			//异常规范接口
			services.AddTransient<FriendlyExceptionFilter>();
			services.AddMvc(options =>
			{
				options.Filters.AddService<SucceededUnifyResultFilter>(99);
				options.Filters.AddService<FriendlyExceptionFilter>(100);

				// 添加应用模型转换器
				options.Conventions.Add(new ApiControllerApplicationModelConvention(services));
			});
		}
	}
}
